import 'package:flutter/material.dart';
import 'package:fsharing/core/viewmodels/pages/mine/user_renew_list_model.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';
import 'package:fsharing/main.dart';
import 'package:fsharing/ui/widgets/background_container.dart';
import 'package:fsharing/ui/widgets/round_back_button.dart';
import 'package:provider/provider.dart';

/// 用户续费信息
class UserRenewPage extends StatelessWidget {
  final int userId;

  UserRenewPage({
    Key key,
    @required this.userId,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => UserRenewListModel(
        Provider.of(context, listen: false),
        userId,
      )..businessType = null,
      child: BackgroundContainer(
        child: Consumer<UserRenewListModel>(
          builder: (context, model, child) {
            switch (model.viewState) {
              case ViewState.busy:
                return Scaffold(
                  appBar: child,
                  body: Center(
                    child: CircularProgressIndicator(),
                  ),
                );
              case ViewState.error:
                return Scaffold(
                  appBar: child,
                  body: Center(
                    child: Text(model.errorMessage),
                  ),
                );
              case ViewState.success:
                return Scaffold(
                  appBar: AppBar(
                    leading: RoundBackButton(),
                    title: Text('续费信息'),
                    actions: <Widget>[
                      FlatButton(
                        onPressed: () {
                          _onTypeClicked(context);
                        },
                        child: Text('续费类型'),
                      )
                    ],
                  ),
                  body: _BodyWidget(),
                );
              default:
                return Container();
            }
          },
          child: AppBar(
            leading: RoundBackButton(),
            title: Text('续费信息'),
          ),
        ),
      ),
    );
  }

  void _onTypeClicked(BuildContext context) async {
    UserRenewListModel model = Provider.of(context, listen: false);
    var result = await showModalBottomSheet(
      context: context,
      builder: (context) {
        return ChangeNotifierProvider.value(
          value: model,
          child: _TypeBottomSheetWidget(),
        );
      },
    );
    if (result is String) {
      model.businessType = result;
    }
  }
}

class _TypeBottomSheetWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    UserRenewListModel model = Provider.of(context, listen: false);
    final list = model.userRenewList.businessTypeOptions.entries.toList();
    return ListView.builder(
      shrinkWrap: true,
      itemBuilder: (context, index) {
        var mapEntry = list[index];
        return ListTile(
          title: Text(mapEntry.value),
          onTap: () {
            Navigator.of(context).pop(mapEntry.key);
          },
        );
      },
      itemCount: list.length,
    );
  }
}

class _BodyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    UserRenewListModel model = Provider.of(context, listen: false);
    final list = model.userRenewList.renewHistories;
    return ListView.builder(
      itemBuilder: (context, index) {
        var renew = list[index];
        return Theme(
          data: lightTheme(),
          child: Material(
            child: ListTile(
              title: Text(renew.businessName),
              subtitle: Text('${renew.businessLevel}  \n金额：${renew.amount}'),
              trailing: Text(renew.createTime),
              isThreeLine: true,
            ),
          ),
        );
      },
      itemCount: list.length,
    );
  }
}
